package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api("员工管理")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;
    private final JwtProperties jwtProperties;

    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录的接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);


        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 员工添加
     *
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result<String> saveEmployee(@RequestBody @Validated EmployeeDTO employeeDTO) {
        employeeService.saveEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页
     * 展示
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页展示")
    public Result<PageResult> pageEmployee(EmployeePageQueryDTO requestParam) {
        PageResult pageResult = employeeService.pageList(requestParam);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询员工信息
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("员工查询")
    public Result<Employee> getEmployeeById(@PathVariable("id")long id) {
//        Employee employee = employeeService.getById(id);
//        return Result.success(employee);
        Employee employee = employeeService.getEmplyeeById(id);
        return Result.success(employee);
    }

    /**
     * 员工禁用启用
     * @return
     */
    @PutMapping
    @ApiOperation("员工更新")
    public Result<Void> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }


    /**
     * 员工禁用启用
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("员工禁用启用")
    public Result<Void> startOrStop(@PathVariable("status") Integer status,@RequestParam("id") long id) {
        employeeService.startOrStop(status,id);
        return Result.success();
    }
}
